package com.viettel.pon.httpclient;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.turbomanage.httpclient.ParameterMap;

public class HttpClientHelper {
//	 duong dan chung
//	private static final String BASE_URL = "http://10.58.71.141:8999";
	
	private static final String BASE_URL = "http://10.58.71.141:9012";
//	private static final String BASE_URL = "http://192.168.208.78:8084";
	
	// tim kiem su co
	private static final String SEARCH_TICKET = "/QLCTKT_TEST/rest/ntmsController/onSearchTicket";
	
	// chi tiet su co
	private static final String SEARCH_TICKET_DETAIL = "/QLCTKT_TEST/rest/ntmsController/onSearchTicketDetail";
	
	// Cap nhat cong viec con
	private static final String UPDATE_CHILD_TASK = "/QLCTKT_TEST/rest/ntmsController/onUpdateChildTask";

	// Tao cong viec con
	private static final String CREATE_CHILD_TASK = "/QLCTKT_TEST/rest/ntmsController/onCreateChildTask";

	// Cap nhat hoan thanh
	private static final String UPDATE_TROUBLE_COMPLETE = "/QLCTKT_TEST/rest/ntmsController/onUpdateTroubleComplete";

	// Cap nhat tam dong
	private static final String UPDATE_TROUBLE_TEMP = "/QLCTKT_TEST/rest/ntmsController/onUpdateTroubleTemp";
	
	// Cap nhat tiendo
	private static final String ON_UPDATE_PROCESS = "/QLCTKT_TEST/rest/ntmsController/onUpdateProcess";

	// Cap nhat su co
	private static final String PRE_UPDATE = "/QLCTKT_TEST/rest/ntmsController/onPreUpdate";
	
	// chuan bi tao cong viec con
	private static final String onPreInsertChildTask = "/QLCTKT_TEST/rest/ntmsController/onPreInsertChildTask";
	
	//--- giao su co cho nhan vien ----//
	private static final String onAssignTicket = "/QLCTKT_TEST/rest/ntmsController/onAssignTicket";
	
	//--- lay su co cc ----//
	private static final String onGetReasonCC = "/QLCTKT_TEST/rest/ntmsController/onGetReasonCC";
		
		
	//------------------ dich vu co dinh -----------------//
	
	// chi tiet cong viec
	private static final String onSearchDVCDDetail = "/QLCTKT_TEST/rest/dvcdController/onSearchDVCDDetail";
	
	// chuan bi cap nhat cong viec khao sar
	private static final String onPreUpdateKSDVCD = "/QLCTKT_TEST/rest/dvcdController/onPreUpdateKSDVCD";
	
	// cap nhat cong viec khao sat
	private static final String onUpdateKSDVCD = "/QLCTKT_TEST/rest/dvcdController/onUpdateKSDVCD";
	
	// cap nhat tam hoan
	private static final String onUpdateDelayChildTask = "/QLCTKT_TEST/rest/dvcdController/onUpdateDelayChildTask";
	
	// cap nhat cong viec con ( keo cap/ han noi)
	private static final String onUpdateChildTask = "/QLCTKT_TEST/rest/dvcdController/onUpdateChildTask";
	
	// cap nhat cong viec nghiem thu ki thuat
	private static final String onUpdateTKDVCD = "/QLCTKT_TEST/rest/dvcdController/onUpdateTKDVCD";
	
	// cap nhat cong viec con dau noi tong dai
	private static final String onUpdateDeloyTask = "/QLCTKT_TEST/rest/dvcdController/onUpdateDeloyTask";
	
	// chuan bi cap nhat cong viec li do ton
	private static final String onPreUpdateTKDVCD = "/QLCTKT_TEST/rest/dvcdController/onPreUpdateTKDVCD";
	
	// ---- Man Hinh Test Dich Vu ---- //
	// load spin Indoor
	private static final String getListOdfIndoor = "/QLCTKT_TEST/rest/dvcdController/getListOdfIndoor";
	
	// load spin Outdoor
	private static final String getOdfOutdoorList = "/QLCTKT_TEST/rest/dvcdController/getOdfOutdoorList";
	
	// load spin Coupler
	private static final String getCoupler = "/QLCTKT_TEST/rest/dvcdController/getCoupler";
	
	// cap nhat cong viec con test dich vu
	private static final String onUpdateTestTask = "/QLCTKT_TEST/rest/dvcdController/onUpdateTestTask";
	private static final String onGetStationByTaskId = "/QLCTKT_TEST/rest/dvcdController/onGetStationByTaskId";
	private static final String onDeviceByNetworkClass = "/QLCTKT_TEST/rest/dvcdController/getListDevice";
	private static final String onGetStationByDept = "/QLCTKT_TEST/rest/dvcdController/onGetStationByDept";
	
	
	// ---- login ---- //
	private static final String login = "/QLCTKT_TEST/rest/authen/login";
	
	// home 
	private static final String onHomePage = "/QLCTKT_TEST/rest/ntmsController/onHomePage";
            
    
    
    // dang nhap qua mail hoac so dt
    
    // khai bao context
    private Context context;
    
    // khai bao cac thanh phan thao tac client va server
    private static AsyncHttpClient client = new AsyncHttpClient();
    private AsyncHttpResponseHandler  asyncHttpResponseHandler;
    
    
    
    
    
    // khai bao file luu tam
//    private PreferenceHelper preferenceHelper;
    
    // contructor
    public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler, Context context) {
            this.asyncHttpResponseHandler = asyncHttpResponseHandler;
            this.context = context;
//            preferenceHelper = new PreferenceHelper(context);
    }
    
    public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler) {
            this.asyncHttpResponseHandler = asyncHttpResponseHandler;
    }
    
    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
            try {
                    client.get(getAbsoluteUrl(BASE_URL, url), params, responseHandler);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }
    
    // phan rieng
    public static void get(String url, AsyncHttpResponseHandler responseHandler) {
            try {
                    client.get(url, null, responseHandler);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
            try {
                    client.post(getAbsoluteUrl(BASE_URL,url), params, responseHandler);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }
    
    
    // get url voi tham so truyen len
    private static String getUrlWithParams(String base, String relativeUrl, List<BasicNameValuePair> params) {
            String url = getAbsoluteUrl(base, relativeUrl);
            for (int i = 0; i < params.size(); i++) {
                    BasicNameValuePair b = params.get(i);
                    url+="&"+b.getName()+"="+b.getValue();
            }
            url = url.replace(" ", "%20");
            return url;
    }
    
    // get url post
    private static String getUrlPostWithParams(String base, String relativeUrl, RequestParams params) {
            String url = getAbsoluteUrl(base, relativeUrl);
            RequestParams r = params;
            url+="?"+r;
            url = url.replace(" ", "%20");
                            
            return url;
    }
    
    
    // tao duong dan api 
    private static String getAbsoluteUrl(String base, String relativeUrl) {
            return base + relativeUrl + "/";
    }
    
    
 // --------search ticket------------//
 	public void searchTicket(long userId, long status, long type){
 		RequestParams params = new RequestParams();
         params.put("userAssignId", String.valueOf(userId));
         params.put("status", String.valueOf(status));
         params.put("type", String.valueOf(type));
         post(SEARCH_TICKET, params, asyncHttpResponseHandler);
 	}
    
    
}
